<?php
class model_help extends base_model{
	private $result = array(
		'state' => 404,
		'message' => 'error',
	);
	
	public function __construct() {
		//默认不用登录
		$this->usercheck = false;
		parent::__construct();
	}
	
	//取出子类下的所有标题
	public function mod_sub_category_list($parentid){
		$parentid = intval($parentid);
		$sql = 'SELECT * FROM `@@news` WHERE `category`='.$parentid.' ORDER BY `sort` ASC';
		$data = $this->db->query($sql,3);
		return $data;
	}
	
	//
	public function mod_help_func($cid,$hid){
		//找出主分类标题
		$sql = "SELECT * FROM `@@newscategory` WHERE `id`={$cid} LIMIT 1";
		$data = $this->db->query($sql,2);
		
		$this->result['h2title'] = $this->clearHtml($data['title']);
		
		$this->result['pagetitle'] = $this->result['h2title'];

		//所有分类及下的文章
		$subsql = "SELECT * FROM `@@newscategory` WHERE `parentid`={$cid} ORDER BY `sort` ASC";
		$subdata = $this->db->query($subsql,3);
		$first_category_id = $subdata[0]['id'];
		
		//取出第一条记录
		if(!$hid){
			$sql = "SELECT * FROM `@@news` WHERE `category`={$first_category_id} ORDER BY `sort` ASC LIMIT 1";
			$data = $this->db->query($sql,2);
		}else{
			$sql = "SELECT * FROM `@@news` WHERE `id`={$hid} LIMIT 1";
			$data = $this->db->query($sql,2);
		}
		$this->result['pagetitle'] = $this->clearHtml($data['title']) .' - '.$this->pageTitle;
		//浏览数加1
		$sql = "UPDATE `@@news` SET `reading`=`reading`+1 WHERE `id`={$hid}";
		$this->db->query($sql,0);
		
		$this->result['state'] = 200;
		$this->result['message'] = 'success';
		$this->result['data'] = $data;
		$this->result['subdata'] = $subdata;
		
		return $this->result;
	}
	
	public function mod_details_func($id){
		//$id = intval($this->get_para[3]);
		$sql = 'SELECT * FROM `@@news` WHERE `id`='.$id;
		$data = $this->db->query($sql,2);
		$this->pageTitle = $this->clearHtml($data['title']);
		//最新公告列表
		$cid = intval($this->config['sysnotice']);
		$pclass_sql = 'select * from `@@news` where state=1 and category='.$cid.' order by sort desc';
		$topsysnotice = $this->db->query($pclass_sql,3);
		$sql = 'SELECT `id`,`title` FROM `@@news` WHERE state=1 and `category`='.$cid.' ORDER BY `id` DESC LIMIT 10';
		$sysnotice = $this->db->query($sql,3);
		//浏览数加1
		$sql = 'UPDATE `@@news` SET `reading`=`reading`+1 WHERE `id`='.$id;
		$this->db->query($sql,0);
		$this->display('help/details',array(
			'data' => $data,
			'sysnotice' => $sysnotice,
			'topsysnotice' => $topsysnotice
		));
	}
}